package com.gjy.jjwt;

import io.jsonwebtoken.Claims;

import java.util.HashMap;
import java.util.Map;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-01-22 10:27:51
 */
public class AuthUtil {

    private static final JwtUtil jwtUtil = new JwtUtil();

    public static Map<String, String> login(String username) {
        // 验证用户身份逻辑省略
        String accessToken = jwtUtil.generateAccessToken(username);
        String refreshToken = jwtUtil.generateRefreshToken(username);

        Map<String, String> tokens = new HashMap<>();
        tokens.put("access_token", accessToken);
        tokens.put("refresh_token", refreshToken);

        return tokens;
    }

    public static Map<String, String> refresh(Map<String, String> token) {
        String refreshToken = token.get("refresh_token");
        Claims claims = jwtUtil.validateToken(refreshToken);

        // 检查refresh_token是否有效（可以添加更多逻辑）
        String username = claims.getSubject();
        String newAccessToken = jwtUtil.generateAccessToken(username);

        Map<String, String> tokens = new HashMap<>();
        tokens.put("access_token", newAccessToken);

        return tokens;
    }

}
